<template>
  <div id="todoVuex">
    <!-- <el-input
      placeholder="请输入任务"
      class="inputClass"
      v-model="inputValue"
      @change="handleInputChange"
      @keyup.enter="addItemToList"
    /> -->
    <input
      placeholder="请输入任务"
      class="inputClass"
      v-model="inputValue"
      @change="handleInputChange"
      @click="addItemToList"
      />
    <el-button type="primary" @click="addItemToList">添加事项</el-button>
      <div class="listClass">
        <ul class="todo-list">
          <li v-for="(item,index) in infolist" :key='index'>
            <div class="view">
              <a-checkbox :checked="item.yes" @change="(e) => { checkStatuschanged(e, item.id) }">{{ item.info }}</a-checkbox>
              <a slot="actions" @click="removeItemById(item.id)" style="float:right;font-size:15px">删除</a>
            </div>
          </li>
        </ul>
      <div slot="footer" class="footer">
        <span>{{ listLength }}条未完成</span>
        <el-button-group>
          <el-button
            :type="viewKey === 'all' ? 'primary' : 'default'"
            @click="changeList('all')"
            >全部</el-button
          >
          <el-button
            :type="viewKey === 'no' ? 'primary' : 'default'"
            @click="changeList('no')"
            >未完成</el-button
          >
          <el-button
            :type="viewKey === 'yes' ? 'primary' : 'default'"
            @click="changeList('yes')"
            >已完成</el-button
          >
        </el-button-group>
        <a @click="clean">清除已完成</a>
      </div>
      </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  data() {
    return {
    }
  },
  created() {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['inputValue', 'viewKey']),
    ...mapGetters(['listLength', 'infolist'])
  },
  methods: {
    // 监听文本框内容变化
    handleInputChange(e) {
      this.$store.commit('setInputValue', e.target.value)
    },
    // 向列表中新增 item 项
    addItemToList() {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.warning('请输入内容！')
      }

      this.$store.commit('addItem')
    },
    // 很据Id删除对应的任务事项
    removeItemById(id) {
      this.$store.commit('removeItem', id)
    },
    // 监听复选框选中状态变化的事件
    checkStatuschanged(e, id) {
      // 通过 e.target.checked 可以接受到最新的选中状态
      // console.log(e.target.checked)
      console.log('==>', id)
      const param = {
        id: id,
        status: e.target.checked
      }
      this.$store.commit('changeStatus', param)
    },
    // 清除已完成的任务
    clean() {
      this.$store.commit('cleanDone')
    },
    // 修改页面上展示的列表数据
    changeList(key) {
      // console.log(key)
      this.$store.commit('changeViewKey', key)
    }
  }
}
</script>

<style scoped>
#todoVuex {
  width: 650px;
  height: 100%;
  margin: 50px auto;
}

.inputClass {
  width: 500px;
  height: 40px;
  margin-right: 10px;
}

.listClass {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.todo-list {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 420px;
  overflow: auto;
}

.todo-list li {
  position: relative;
  font-size: 35px;
  border-bottom: 1px solid #ededed;
  height: 60px;
  box-sizing: border-box;
}

.todo-list li:last-child {
  border-bottom: none;
}
.view a{
  cursor: pointer;
  color: red;
}
</style>
